package ldh.common.testui.handle.impl;

import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIcon;
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView;
import javafx.event.ActionEvent;
import javafx.scene.Parent;
import javafx.scene.control.*;
import ldh.common.testui.constant.TreeNodeType;
import ldh.common.testui.handle.BaseTreeNodeService;
import ldh.common.testui.model.ParamModel;
import ldh.common.testui.model.TreeNode;
import ldh.common.testui.util.RegionUtil;
import ldh.common.testui.util.TreeUtil;

import java.util.ArrayList;
import java.util.List;

public class ProjectTreeNodeService extends BaseTreeNodeService {

    public ProjectTreeNodeService(TreeView<TreeNode> treeView, TabPane tabPane, ContextMenu contextMenu, TreeNodeType treeNodeType) {
        super(treeView, tabPane, contextMenu, treeNodeType);
    }

    @Override
    protected void mouseClicked(TreeItem<TreeNode> treeItem) {
        if (treeItem == null || treeItem.getValue() == null) return;
        if (treeItem.getValue().getTreeNodeType() != treeNodeType) return;
    }

    @Override
    public List<MenuItem> buildNodeMenuItem(TreeItem<TreeNode> treeItem) {
        List<MenuItem> result = new ArrayList<>();
//        ParamModel springParamModel = SpringInitFactory.getInstance().getSpringParamModel(treeItem);
//        if (springParamModel != null) {
//            MenuItem runSpring = new MenuItem("运行Spring", buildGraphic(new MaterialDesignIconView(MaterialDesignIcon.PLAY)));
//            runSpring.setOnAction(this::runSpring);
//            result.add(runSpring);
//        }

        if (!TreeUtil.hasTreeNodeType(treeItem, TreeNodeType.Node)) {
            MenuItem addTest = new MenuItem("添加测试场景", buildGraphic(new MaterialDesignIconView(MaterialDesignIcon.PLUS)));
            addTest.setOnAction(te->addCase(te));
            result.add(addTest);

            if (!TreeUtil.hasTreeNodeType(treeItem, TreeNodeType.Param)) {
                MenuItem addParam = new MenuItem("添加属性", buildGraphic(new MaterialDesignIconView(MaterialDesignIcon.PARKING)));
                addParam.setOnAction(this::addParam);
                result.add(addParam);
            }
        } else {
            MenuItem addGroup = new MenuItem("添加组", buildGraphic(new MaterialDesignIconView(MaterialDesignIcon.PLUS)));
            addGroup.setOnAction(e0->addGroup(TreeNodeType.Node, false, null));
            result.add(addGroup);
        }
        return result;
    }

    private void addCase(ActionEvent actionEvent) {
        addGroup(TreeNodeType.Case, false, null);
    }

    private void addParam(ActionEvent actionEvent) {
        createTreeItem(TreeNodeType.Param, "属性管理", 1, (treeItem)->{
            Tab tab = new Tab("属性管理");
            tab.setUserData(treeItem.getValue());

            tabPane.getTabs().add(tab);
            tabPane.getSelectionModel().select(tab);
            Parent parent = RegionUtil.paramPane(treeItem);
            tab.setContent(parent);
            return "";
        });
    }
}
